package com.ruoyi.monitormanage.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.monitormanage.mapper.TReviewgpsMapper;
import com.ruoyi.monitormanage.domain.TReviewgps;
import com.ruoyi.monitormanage.service.ITReviewgpsService;

/**
 * 生成审核pgsService业务层处理
 * 
 * @author admin
 * @date 2025-10-22
 */
@Service
public class TReviewgpsServiceImpl implements ITReviewgpsService 
{
    @Autowired
    private TReviewgpsMapper tReviewgpsMapper;

    /**
     * 查询生成审核pgs
     * 
     * @param id 生成审核pgs主键
     * @return 生成审核pgs
     */
    @Override
    public TReviewgps selectTReviewgpsById(Long id)
    {
        return tReviewgpsMapper.selectTReviewgpsById(id);
    }

    /**
     * 查询生成审核pgs列表
     * 
     * @param tReviewgps 生成审核pgs
     * @return 生成审核pgs
     */
    @Override
    public List<TReviewgps> selectTReviewgpsList(TReviewgps tReviewgps)
    {
        return tReviewgpsMapper.selectTReviewgpsList(tReviewgps);
    }

    /**
     * 新增生成审核pgs
     * 
     * @param tReviewgps 生成审核pgs
     * @return 结果
     */
    @Override
    public int insertTReviewgps(TReviewgps tReviewgps)
    {
        String currentDateTime = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        tReviewgps.setCreatetime(currentDateTime);
        SysDept sysDept = tReviewgpsMapper.selectSysDept(tReviewgps.getDeptid());
        tReviewgps.setTestpoint(sysDept.getDeptName());
        String ancestors = sysDept.getAncestors();
        List<String> originalList = new ArrayList<>(Arrays.asList(ancestors.split(",")));
        List<String> filteredList = new ArrayList<>(originalList.subList(2, originalList.size()));
        for (String item : filteredList) {
            SysDept sysDeptVo = tReviewgpsMapper.selectSysDept(Long.valueOf(item));
            if (sysDeptVo.getOrganizationcode().length() == 2){
                tReviewgps.setSheng(sysDeptVo.getDeptName());
            } else if (sysDeptVo.getOrganizationcode().length() == 4) {
                tReviewgps.setShi(sysDeptVo.getDeptName());
            }else if (sysDeptVo.getOrganizationcode().length() == 6) {
                tReviewgps.setQu(sysDeptVo.getDeptName());
            }
        }
        return tReviewgpsMapper.insertTReviewgps(tReviewgps);
    }

    /**
     * 修改生成审核pgs
     * 
     * @param tReviewgps 生成审核pgs
     * @return 结果
     */
    @Override
    public int updateTReviewgps(TReviewgps tReviewgps)
    {
        return tReviewgpsMapper.updateTReviewgps(tReviewgps);
    }

    /**
     * 批量删除生成审核pgs
     * 
     * @param ids 需要删除的生成审核pgs主键
     * @return 结果
     */
    @Override
    public int deleteTReviewgpsByIds(Long[] ids)
    {
        return tReviewgpsMapper.deleteTReviewgpsByIds(ids);
    }

    /**
     * 删除生成审核pgs信息
     * 
     * @param id 生成审核pgs主键
     * @return 结果
     */
    @Override
    public int deleteTReviewgpsById(Long id)
    {
        return tReviewgpsMapper.deleteTReviewgpsById(id);
    }
}
